package action.main;

import java.util.ArrayList;
import java.util.List;

import bean.Pagination;

import dao.BookDAO;
import dao.CategoryDAO;

import util.ObjectFactory;

import entity.Book;
import entity.Category;

public class CopyOfBookListAction {
	//input
	private int page=1;
	private int cid;//子ID
	private int pid;//父ID
	private String value;
	//output
	private List<Book> books;
	private List<Category> cates;
	private int count; //总数量
	private int totalPage;
	private List<String> cateNames;//栏目导航信息
	
	public String execute() throws Exception{
		CategoryDAO cateDAO = ObjectFactory.getInstance(CategoryDAO.class);
		BookDAO dao = ObjectFactory.getInstance(BookDAO.class);
		cates=cateDAO.findByParentId(pid);
		//添加栏目导航
		cateNames = new ArrayList<String>();
		Category ca = cateDAO.findById(cid);
		cateNames.add(ca.getName());
		if(ca.getParentId()!=1){
			String name= cateDAO.findById(ca.getParentId()).getName();
			cateNames.add(0, name);
		}
		//设置总数
		for(Category c:cates){
			count+=c.getPnum();
		}
		//设置分页
		Pagination p = new Pagination();
		p.setPage(page);
		if(value==null || value==""){
			books=dao.findById(cid,p);
		}else{
			String[] str = value.split(",");
			System.out.println(str[0]);
			System.out.println(str[1]);
			books=dao.findByProperty(str[0], str[1], cid, p);
		}
		int count1=dao.findById(cid).size();
		if(count1%p.getPageSize()==0){
			totalPage=count1/p.getPageSize();
		}else{
			totalPage=count1/p.getPageSize()+1;
		}
		return "success";
	}

	public int getCid() {
		return cid;
	}

	public void setCid(int cid) {
		this.cid = cid;
	}

	public List<Book> getBooks() {
		return books;
	}

	public void setBooks(List<Book> books) {
		this.books = books;
	}

	public List<Category> getCates() {
		return cates;
	}

	public void setCates(List<Category> cates) {
		this.cates = cates;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public List<String> getCateNames() {
		return cateNames;
	}

	public void setCateNames(List<String> cateNames) {
		this.cateNames = cateNames;
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
	
}
